using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace SysCommon.UI
{
    public partial class LoadingForm : Form
    {
        private delegate void NoneParamMethod();

        private static int s_ShowCount;
        private static LoadingForm s_PreInstance;

        static LoadingForm()
        {
            //Application.ThreadExit += new EventHandler(Application_ThreadExit);
        }

        private static void Application_ThreadExit(object sender, EventArgs e)
        {
            while (s_ShowCount > 0)
            {
                DoneLoading();
            }
        }

        public static void ShowLoading(string description)
        {
            return;
            if (s_ShowCount < 0)
            {
                s_ShowCount = 0;
            }

            if (s_ShowCount == 0)
            {
                //Thread showThread = new Thread(new ThreadStart(delegate()
                //{
                    s_PreInstance = new LoadingForm(description);
                    s_PreInstance.Top = 372;
                    s_PreInstance.Left = 12;
                    s_PreInstance.ShowDialog();
                //}
            //));

                //showThread.Start();
            }
            else
            {
                if (s_PreInstance != null && !s_PreInstance.IsDisposed && s_PreInstance.IsHandleCreated)
                    //s_PreInstance.Invoke(new NoneParamMethod(delegate()
                    //{
                        s_PreInstance.label1.Text = description;
                    //}));
            }

            s_ShowCount++;
        }

        public static void ShowLoading(string description,Boolean isShow)
        {
            
            if (s_ShowCount < 0)
                s_ShowCount = 0;

            if (s_ShowCount == 0)
            {
                //Thread showThread = new Thread(new ThreadStart(delegate()
                //{
                    s_PreInstance = new LoadingForm(description);
                    s_PreInstance.Top = 372;
                    s_PreInstance.Left=18;
                    s_PreInstance.ShowDialog();
                //}));

                //showThread.Start();
            }
            else
            {
                if (s_PreInstance != null )
                    s_PreInstance.Invoke(new NoneParamMethod(delegate()
                    {
                        s_PreInstance.label1.Text = description;
                    }));
            }

            s_ShowCount++;
        }
     
        public static void DoneLoading()
        {
            return;
            if (s_ShowCount > 0)
                s_ShowCount--;
            else
                s_ShowCount = 0;
        }
        public static void DoneLoading(Boolean isShow)
        {
   
            if (s_ShowCount > 0)
                s_ShowCount--;
            else
                s_ShowCount = 0;
        }

        private LoadingForm(string description)
        {
            InitializeComponent();

            this.loadingCircle1.NumberSpoke = 15;
            this.loadingCircle1.SpokeThickness = 4;
            this.loadingCircle1.InnerCircleRadius = 10;
            this.loadingCircle1.OuterCircleRadius = 25;
            this.loadingCircle1.m_Timer.Tick += new EventHandler(m_Timer_Tick);

            this.loadingCircle1.Active = true;

            this.label1.Text = description;

            this.Locate();
            this.TopMost = true;
        }

        private void m_Timer_Tick(object sender, EventArgs e)
        {
            if (s_ShowCount == 0)
                this.Close();
        }

        private void Locate()
        {
            const int c_Width = 1280;
            const int c_Height = 1024;

            int left = (c_Width - this.Width) / 2;
            int top = (c_Height - this.Height) / 2;

            this.Location = new Point(left, top);
        }

        private void loadingCircle1_DoubleClick(object sender, EventArgs e)
        {
            this.Close();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            s_ShowCount = 0;
        }
    }
}